package com.petmagics.learnlayoutanimation;

import android.animation.LayoutTransition;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.LinearLayout;

/**
 * 布局改变动画
 */
public class LayoutChangeAnimationFm extends Fragment {

    private LinearLayout root;

    private View.OnClickListener btnOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(view.getId() != R.id.btnAddButton) {
                root.removeView(view);
            }
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        root = (LinearLayout) inflater.inflate(R.layout.fragment_layoutchange_animation, container, false);

        root.findViewById(R.id.btnBack).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getFragmentManager().popBackStack();
            }
        });

        root.findViewById(R.id.btnAddButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addButton();
            }
        });

        return root;
    }

    private void addButton() {
        Button btn = new Button(getActivity());
        btn.setText("Remove me");
        root.addView(btn);

        ScaleAnimation sa = new ScaleAnimation(0,1,0,1);
        sa.setDuration(350);
        LayoutTransition lt = new LayoutTransition();
//        lt.setDuration(3000);
        root.setLayoutTransition(lt);
        btn.setOnClickListener(btnOnClickListener);
    }
}
